﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Threading;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

using PickGold;
using PickGold.Api;
using PickGold.Base;
using PickGold.Collections;

using BaseCommon = PickGold.Base.Common;

namespace PickGold.Base.OCR
{
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	public class ColorEquals
	{
		internal byte _LA, _LR, _LG, _LB, _RA, _RR, _RG, _RB;

		public ColorEquals(Color value) : this(value.A, value.R, value.G, value.B, Color.White.A, Color.White.R, Color.White.G, Color.White.B) { }

		public ColorEquals(Color left, Color right) : this(left.A, left.R, left.G, left.B, right.A, right.R, right.G, right.B) { }

		public ColorEquals(byte a, byte r, byte g, byte b) : this(a, r, g, b, Color.White.A, Color.White.R, Color.White.G, Color.White.B) { }

		public ColorEquals(byte la, byte lr, byte lg, byte lb, byte ra, byte rr, byte rg, byte rb)
		{
			this._LA = la;
			this._LR = lr;
			this._LG = lg;
			this._LB = lb;

			this._RA = ra;
			this._RR = rr;
			this._RG = rg;
			this._RB = rb;
		}

		public bool Equals(Color color)
		{
			return this.Equals(color.A, color.R, color.G, color.B);
		}

		public bool Equals(byte a, byte r, byte g, byte b)
		{
			if (this._LA > a)
				return false;

			if (a > this._RA)
				return false;

			if (this._LR > r)
				return false;

			if (r > this._RR)
				return false;

			if (this._LG > g)
				return false;

			if (g > this._RG)
				return false;

			if (this._LB > b)
				return false;

			if (b > this._RB)
				return false;

			return true;
		}

		public static bool Equals(ColorEquals[] ces, Color color)
		{
			return ColorEquals.Equals(ces, color.A, color.R, color.G, color.B);
		}

		public static bool Equals(ColorEquals[] ces, byte a, byte r, byte g, byte b)
		{
			if (ces == null)
				return false;

			for (var i = 0; i < ces.Length; i++)
			{
				if (a >= ces[i]._LA && ces[i]._RA >= a &&
					r >= ces[i]._LR && ces[i]._RR >= r &&
					g >= ces[i]._LG && ces[i]._RG >= g &&
					b >= ces[i]._LB && ces[i]._RB >= b)
					return true;
			}

			return false;
		}
	}
}
